package ThreadAsynchronous;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Author  zxy
 * Date  2023/1/3 下午 3:02
 */
public class AsyncThread extends Thread{

    @Override
    public void run() {
        System.out.println("当前线程名称：" + this.getName() +
                ", 执行线程名称：" + Thread.currentThread().getName() + "-hello");
    }

    private ExecutorService executor = Executors.newCachedThreadPool() ;

    public void fun() throws Exception {
        executor.submit(new Runnable(){
            @Override
            public void run() {
                try {
                    //要执行的业务代码，我们这里没有写方法，可以让线程休息几秒进行测试
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + "睡够啦~");
                } catch(Exception e) {
                    throw new RuntimeException("报错啦！！");
                }
            }
        });
    }


    public static void main(String[] args) throws Exception {
        // 异步线程
        AsyncThread asyncThread1 = new AsyncThread();
        AsyncThread asyncThread2 = new AsyncThread();
        asyncThread1.start();
        asyncThread2.start();

        // 线程池
        asyncThread1.fun();
        //asyncThread2.fun();
    }

}
